package org.moonshiner.controller.security.signature;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;

public class SignGenerator {

	public void generate(String name) {

		KeyPairGenerator generator = null;
		try {
			generator = KeyPairGenerator.getInstance("DSA", "SUN");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}

		SecureRandom random = null;
		try {
			random = SecureRandom.getInstance("SHA1PRNG", "SUN");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}

		generator.initialize(1024, random);

		KeyPair keyPair = generator.generateKeyPair();

		PrivateKey privateKey = keyPair.getPrivate();

		PublicKey publicKey = keyPair.getPublic();

		Signature signature = null;

		try {
			signature = Signature.getInstance("SHA1withDSA", "SUN");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}

		try {
			signature.initSign(privateKey);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}

		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream("1.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedInputStream bis = new BufferedInputStream(inputStream);
		byte[] buffer = new byte[1024];
		int length;
		try {
			while ((length = bis.read(buffer)) >= 0) {
				signature.update(buffer, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SignatureException e) {
			e.printStackTrace();
		} finally {
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		byte[] sign = new byte[0];
		try {
			sign = signature.sign();
		} catch (SignatureException e) {
			e.printStackTrace();
		}

		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream("signature.sig");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			fileOutputStream.write(sign);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		byte[] pubKeyByte = publicKey.getEncoded();

		try {
			fileOutputStream = new FileOutputStream("publicKey.key");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			fileOutputStream.write(pubKeyByte);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
